package com.edu.cqr.web.demo.service.impl;

import com.edu.cqr.web.demo.mapper.RoleInfoMapper;
import com.edu.cqr.web.demo.mapper.UserInfoMapper;
import com.edu.cqr.web.demo.mapper.UserRoleMapper;
import com.edu.cqr.web.demo.model.UserInfo;
import com.edu.cqr.web.demo.model.UserRole;
import com.edu.cqr.web.demo.service.UserService;
import org.apache.catalina.User;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RoleInfoMapper roleInfoMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public UserInfo getUserInfo(String userName) {
        return userInfoMapper.findFirstByUserName(userName);
    }

    @Override
    public Set<String> getRoles(Integer userId) {
        List<String> roleNameList = roleInfoMapper.queryRoleNameByUserId(userId);
        Set<String> roles = new HashSet<>(roleNameList);
        return roles;
    }

    @Override
    public void addUser(String userName, String password, String roleName) throws Exception {
        Integer roleId = roleInfoMapper.findRoleIdByRoleName(roleName);
        if(roleId == null){
            throw new Exception();
        }
        String salt = UUID.randomUUID().toString().replace("-","");
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME,password,salt,3);

        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setSalt(salt);
        userInfo.setPassword(simpleHash.toString());
        userInfo.setCreateTime(new Date());
        userInfo.setLastTime(null);
        userInfo.setUserState(Byte.parseByte("0"));
        userInfoMapper.insert(userInfo);

        Integer userId = userInfoMapper.findFirstByUserName(userName).getUserId();
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        userRoleMapper.insert(userRole);
    }

    @Override
    public void delUser(String userName) throws Exception {
        Integer userId=userInfoMapper.findFirstByUserName(userName).getUserId();
        if (userId == null){
            throw new Exception();
        }
        userRoleMapper.delUserRoleInfoByUserId(userId);
        userInfoMapper.delUserInfoByUserId(userId);
    }

    @Override
    public void modifyPassword(String userName, String password) {
        UserInfo userInfo = userInfoMapper.findFirstByUserName(userName);
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME,password,userInfo.getSalt(),3);
        userInfoMapper.updatePasswordByUserName(simpleHash.toString(),userName);
    }

    @Override
    public boolean verifyPassword(String password, String salt, String realPassword) {
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME,password, salt,3);
        if(realPassword.equals(simpleHash.toString())){
            return true;
        }else {
            return false;
        }
        }

    @Override
    public void updateState(String userName) {
        userInfoMapper.updateStateByUserName(userName,2);
    }

    @Override
    public void enbupdateState(String userName) {
        userInfoMapper.enbupdateStateByUserName(userName,0);
    }

    @Override
    public void updateRoles(String userName, String roleName) {
        UserInfo userInfo = userInfoMapper.findFirstByUserName(userName);
        Integer roleId = roleInfoMapper.findRoleIdByRoleName(roleName);
        userRoleMapper.updateRoleInfoByUserId(userInfo.getUserId(),roleId);
    }

    @Override
    public UserInfo findUserInfo(String userName) {
        return userInfoMapper.findFirstByUserName(userName);
    }

    @Override
    public List<UserInfo> findUserInfoAll() {
        return userInfoMapper.findUserInfoAll();
    }


}
